package com.gzsxy.sso.common.utils;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import java.util.List;

/**
 * @author xiaolong
 * @version 1.0
 * @description: 中文转拼音
 * @date 2021/11/16 15:58
 */
public class StringUtils {

    /**
     * 得到 全拼
     *
     * @param src
     * @return
     */
    public static String getPingYin(String src) {
        char[] t1 = null;
        //转为字符数组
        t1 = src.toCharArray();
        String[] t2 = new String[t1.length];
        HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
        //UPPERCASE,  全部大写
        //LOWERCASE,  全部小写
        //FIRSTUPPER 首字母大写
        t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        //WITH_TONE_NUMBER 用数字表示声调，例如：liu2
        //WITHOUT_TONE 无声调表示，例如：liu
        //WITH_TONE_MARK 用声调符号表示，例如：liú 设置特殊拼音ü的显示格式：
        t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        //.WITH_U_AND_COLON 以U和一个冒号表示该拼音，例如：lu:
        //WITH_V 以V表示该字符，例如：lv
        //WITH_U_UNICODE 以ü表示
        t3.setVCharType(HanyuPinyinVCharType.WITH_V);
        String t4 = "";
        int t0 = t1.length;
        try {
            for (int i = 0; i < t0; i++) {
                // 判断是否为汉字字符
                if (Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) {
                    t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
                    t4 += t2[0];
                } else {
                    t4 += Character.toString(t1[i]);
                }
            }
            return t4;
        } catch (BadHanyuPinyinOutputFormatCombination e1) {
            e1.printStackTrace();
        }
        return t4;
    }


    /**
     * 得到中文首字母
     *
     * @param str
     * @return
     */
    public static String getPinYinHeadChar(String str) {

        HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
        //全部大写
        t3.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        String convert = "";
        try {
            for (int j = 0; j < str.length(); j++) {
                char word = str.charAt(j);
                //转为拼音
                String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word,t3);
                if (pinyinArray != null) {
                    //第一个字拼音第一个字符
                    convert += pinyinArray[0].charAt(0);
                } else {
                    convert += word;
                }
            }
        } catch (BadHanyuPinyinOutputFormatCombination e1) {
            e1.printStackTrace();
        }
        return convert;
    }

    /**
     * 将字符串转移为ASCII码
     *
     * @param cnStr
     * @return
     */
    public static String getCnASCII(String cnStr) {
        StringBuffer strBuf = new StringBuffer();
        byte[] bGBK = cnStr.getBytes();
        for (int i = 0; i < bGBK.length; i++) {
// System.out.println(Integer.toHexString(bGBK[i]&0xff));
            strBuf.append(Integer.toHexString(bGBK[i] & 0xff));
        }
        return strBuf.toString();
    }




    /**
     * @description: 用指定的分隔符把字符串合并成为一个字符串，数组为null或长度为0 返回空字符串
     * @param strList 带合并的字符串集合
     * @return  合并后字符串
     */
    public static String listToString(List<String> strList){
        return listToString(strList,",");
    }

    /**
     * @description: 用指定的分隔符把字符串合并成为一个字符串，数组为null或长度为0 返回空字符串
     * @param strList 带合并的字符串集合
     * @param splitStr 分隔符
     * @return  合并后字符串
     */
    public static String listToString(List<String> strList,String splitStr){
        if (strList == null || strList.size() == 0){
            return "";
        }
        if (splitStr == null || splitStr.length() == 0){
            splitStr = "";
        }
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < strList.size() - 1; i++) {
            buffer.append(strList.get(i)).append(splitStr);
        }
//        //最后一个列表元素添加进入buffer
        buffer.append(strList.get(strList.size() - 1));
        return buffer.toString();
    }


    public static void main(String[] args) {
        String cnStr = "张晓龙";
        System.out.println(getPingYin(cnStr));
        System.out.println(getPinYinHeadChar(cnStr));
    }
}
